package org.example;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.io.IOException;


public class BothClassInOneClassLoader {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException, IOException {

        MyClassLoader myClassLoader = new MyClassLoader();
        Class aClass = myClassLoader.defineClass("org.example.Example", Generator.getModifedClass());
        System.out.println(aClass.getSimpleName());
        Class bClass = myClassLoader.defineClass("org.Test",Generator.getBytecodeForClass());

        Constructor constructor = bClass.getConstructor();
        Object o = constructor.newInstance();

        Method method = aClass.getMethod("run", String.class);
        method.invoke(o,"test");

        try{
            Example e = (Example) o;
            e.run("test");
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        Example example = new Example();
        example.run("test");
    }
}
